{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "47447578-544a-4e1c-8893-a81751654b11",
   "metadata": {},
   "source": [
    "# 流程控制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cba14be-a0db-46ec-b12c-35afd3b66561",
   "metadata": {},
   "source": [
    "Python 中的流程控制包括条件语句和循环语句。条件语句用于根据条件执行不同的代码块，而循环语句则用于重复执行一段代码。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9925235-6ee6-49dc-82dd-337975c98fab",
   "metadata": {},
   "source": [
    "## 条件判断 if语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47ad9b86-5084-479d-85a6-0294f532e69c",
   "metadata": {},
   "source": [
    "![img](https://www.runoob.com/wp-content/uploads/2013/11/if-condition.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "389d9ad9-e3be-4e0e-811b-15f263bc8a31",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "age = -5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f9af7be7-6cc5-4d09-972c-b96ea5f95531",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是在逗我吧!\n"
     ]
    }
   ],
   "source": [
    "if age <= 0:\n",
    "    print(\"你是在逗我吧!\")\n",
    "else:\n",
    "    print(\"相当于 14 岁的人。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4eaef70c-cf4b-451c-9646-0cae2b667094",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "age = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f3008933-bd5c-4627-aec5-d3e09808f504",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age <= 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f6d479c4-5227-42f2-932d-fcfcab84f653",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bool"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(age <= 0)    # 这个是布尔类型  只要 True False 这两个值可以取用   对应数字  非零  和 零"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "1f722dc6-0e21-431f-ba93-9de9ca6a0810",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "if 8:\n",
    "    print(\"1\")\n",
    "else:\n",
    "    print(\"0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "1ca525a3-ddbd-470b-b46d-7c93a5e7bd41",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "未成年防沉迷。\n"
     ]
    }
   ],
   "source": [
    "if age <= 0:\n",
    "    print(\"你是在逗我吧!\")\n",
    "elif age < 18:\n",
    "    print(\"未成年防沉迷。\")\n",
    "else:\n",
    "    print(\"哦豁，一不小心长大了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a07bae49-9e1d-4a70-a390-ed925e0b3a15",
   "metadata": {},
   "source": [
    "### 常见条件"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9c4ba9d-fd1a-4bd1-95f5-d219f67af3f8",
   "metadata": {},
   "source": [
    "| 操作符 | 描述                     |\n",
    "| :----- | :----------------------- |\n",
    "| `<`    | 小于                     |\n",
    "| `<=`   | 小于或等于               |\n",
    "| `>`    | 大于                     |\n",
    "| `>=`   | 大于或等于               |\n",
    "| `==`   | 等于，比较两个值是否相等 |\n",
    "| `!=`   | 不等于                   |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "839f0a0c-604b-43a7-83bd-d75688df9eca",
   "metadata": {},
   "source": [
    "### if 嵌套"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e32550a-203d-4d2d-a790-895bc6600389",
   "metadata": {},
   "source": [
    "还是学好,上面的单个判断的使用，就可以了，后面的就是在上面的基础上多写一个if  这个就是if里面放if  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40ebfc0e-e7ed-442d-8dbd-57bd70f3735d",
   "metadata": {},
   "source": [
    "``` python\n",
    "if 表达式1:\n",
    "    语句\n",
    "    if 表达式2:\n",
    "        语句\n",
    "    elif 表达式3:\n",
    "        语句\n",
    "    else:\n",
    "        语句\n",
    "elif 表达式4:\n",
    "    语句\n",
    "else:\n",
    "    语句\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5e3238ec-f63f-44ec-816c-9bd3c661695a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "输入一个数字： 50\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你输入的数字可以整除 2，但不能整除 3\n"
     ]
    }
   ],
   "source": [
    "num=int(input(\"输入一个数字：\"))\n",
    "if num%2==0:\n",
    "    if num%3==0:\n",
    "        print (\"你输入的数字可以整除 2 和 3\")\n",
    "    else:\n",
    "        print (\"你输入的数字可以整除 2，但不能整除 3\")\n",
    "else:\n",
    "    if num%3==0:\n",
    "        print (\"你输入的数字可以整除 3，但不能整除 2\")\n",
    "    else:\n",
    "        print  (\"你输入的数字不能整除 2 和 3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074a11df-1d4e-4f4a-a324-4084ac3011b5",
   "metadata": {
    "tags": []
   },
   "source": [
    "![图片](https://gitee.com/lmq886/image-warehouse/raw/master/img/数据结构.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
